package com.ssy.lingxi.scheduler.controller;

import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.scheduler.api.model.*;
import com.ssy.lingxi.scheduler.service.IScheduleTaskService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 定时任务相关接口
 * @author 万宁
 * @version 2.0.0
 * @date 2021-07-12
 */
@RestController
@RequestMapping("/schedule/task")
public class ScheduleTaskController {

    @Resource
    private IScheduleTaskService scheduleTaskService;

    /**
     * 创建定时任务
     * @param taskVO 接口参数
     * @return 定时任务的Id
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Wrapper<Long> createScheduleTask(@RequestBody @Valid ScheduleTaskVO taskVO) {
        return scheduleTaskService.createScheduleTask(taskVO);
    }

    /**
     * 创建永久任务
     * @param taskVO 接口参数
     * @return 定时任务的Id
     */
    @RequestMapping(value = "/create/permanent", method = RequestMethod.POST)
    public Wrapper<Long> createPermanentTask(@RequestBody @Valid PermanentTaskVO taskVO) {
        return scheduleTaskService.createPermanentTask(taskVO);
    }

    /**
     * 查询定时任务信息列表
     * @param queryVO 接口参数
     * @return 查询结果
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Wrapper<List<ScheduleTaskDefinitionVO>> listScheduleTask(@RequestBody @Valid ScheduleTaskQueryVO queryVO) {
        return scheduleTaskService.listScheduleTask(queryVO);
    }

    /**
     * 修改任务的启动时间
     * @param updateVO 接口参数
     * @return 修改结果
     */
    @RequestMapping(value = "/time/update", method = RequestMethod.POST)
    public Wrapper<Void> updateScheduleTaskExecTime(@RequestBody @Valid ScheduleTaskUpdateExecTimeVO updateVO) {
        return scheduleTaskService.updateScheduleTaskExecTime(updateVO);
    }

    /**
     * 调用方执行完成定时任务后，通知定时任务模块删除任务
     * @param idVO 接口参数
     * @return 通知结果
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Wrapper<Void> deleteScheduleTask(@RequestBody @Valid ScheduleTaskIdVO idVO) {
        return scheduleTaskService.deleteScheduleTask(idVO);
    }
}
